<template>
  <view class="tab-bar">
    <view v-for="(item, index) in list" :key="index" class="tab-bar-item" @click="switchTab(item, index)">
      <image class="tab_img" :src="selected === index ? item.selectedIconPath : item.iconPath"></image>
      <view class="tab_text" :style="{ color: selected === index ? selectedColor : color }">{{ item.text }}</view>
    </view>
  </view>
</template>

<script>
  import store from '../../store'
  export default {
    props: {
      selected: {
        // 当前选中的tab index
        type: Number,
        default: 0,
      },
    },
    data() {
      return {
        color: '#999999',
        selectedColor: '#333333', 
        userIdentity: null,
        list: [],
        data:{},
        manager: [
          {
            pagePath: "pages/index",
            iconPath: "static/images/tabbar/Billing1.png",
            selectedIconPath: "static/images/tabbar/Billing.png",
            text: "开单"
          },
          {
            pagePath: "pages/work/index",
            iconPath: "static/images/tabbar/staging1.png",
            selectedIconPath: "static/images/tabbar/staging.png",
            text: "工作台"
          },
          {
            pagePath: "pages/order/index",
            iconPath: "static/images/tabbar/statistics1.png",
            selectedIconPath: "static/images/tabbar/statistics.png",
            text: "统计"
          },
          {
            pagePath: "pages/mine/index",
            iconPath: "static/images/tabbar/mine1.png",
            selectedIconPath: "static/images/tabbar/mine.png",
            text: "我的"
          }
        ],
        admin: [
          {
            pagePath: "pages/index",
            iconPath: "static/images/tabbar/Billing1.png",
            selectedIconPath: "static/images/tabbar/Billing.png",
            text: "开单"
          },
          {
            pagePath: "pages/work/index",
            iconPath: "static/images/tabbar/staging1.png",
            selectedIconPath: "static/images/tabbar/staging.png",
            text: "工作台"
          },
          {
            pagePath: "pages/admin/index",
            iconPath: "static/images/tabbar/statistics1.png",
            selectedIconPath: "static/images/tabbar/statistics.png",
            text: "统计"
          },
          {
            pagePath: "pages/mine/index",
            iconPath: "static/images/tabbar/mine1.png",
            selectedIconPath: "static/images/tabbar/mine.png",
            text: "我的"
          }
        ],
      }
    },
    created() {
      const roles = store.getters && store.getters.roles
      // const roles = 'admin'
      this.userIdentity = roles
      this.list = this[roles]
    },

    methods: {
      switchTab(item, index) {
        let url = item.pagePath
        if ( this.userIdentity === 'manager') {
          uni.switchTab({
            url
          })
        } else if (this.userIdentity === 'admin') {
          return 
        } else {
          uni.redirectTo({
            url
          })
        }
      }
    }
  }
</script>

<style lang="scss">
  .tab-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: 120rpx;
    background: white;
    display: flex;
    justify-content: center;
    align-items: center;
    padding-bottom: calc(env(safe-area-inset-bottom) / 2); // 适配iphoneX的底部
    z-index: 9999;

    .tab-bar-item {
      flex: 1;
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;

      .tab_img {
        width: 37rpx;
        height: 41rpx;
      }

      .tab_text {
        font-size: 20rpx;
        margin-top: 9rpx;
      }
    }
  }
</style>
